<template>
  <div style="position:relative">
    <!-- :params="fetchParams" -->
    <FilterTable
      ref="table"
      :columns="columns"
      :dataSource="list"
      :height="200"
      :isColumnFilter="false"
      :isToperInfo="false"
      :isSelectColumn="false"
      :onCellChange="onCellChange"
      :onSyncTableData="syncTableFun"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addFjxmHandle()">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleFjxm" title="附加项目选择" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto' }" destroyOnClose>
      <AddFjxm ref="addFjxm" @closeFj="closeAddfjxm" />
    </BaseDialog>
  </div>
</template>
<script>
import AddFjxm from '../addXM/addfjxm';
import { dictionary } from '@/utils/dictMixin';
import { detailsFjxm, getPartitionList } from '@/api/repair/repair/activitesManage';

let _this;

export default {
  name: 'Fjxm',
  components: {
    AddFjxm
  },
  mixins: [dictionary],
  props: ['type', 'detaildata'],
  data() {
    // this.BaseTable = null;
    return {
      visibleFjxm: false,
      columns: this.createTable(),
      fetchApi: '',
      // fetchParams: {xhrAbort: false},
      list: [],
      tableData: [],
      total: 0
    };
  },
  created() {
    _this = this;
    _this.getPartitionList();
    if (_this.type == 2) {
      _this.detailsFjxm();
    }
  },
  methods: {
    // 表格数据变化时
    onCellChange(val, val2) {
      console.log(val, val2, 'val');
      console.log(val[Object.keys(val)[0]], '修改后对应的编码');
      // ADD_ITEM_AMOUNT附加项目费
      // DISCOUNT折扣率
      // RECEIVE_AMOUNT折扣后附加项目费
      if (isNaN(Number(this.list[val2.$index].DISCOUNT)) || isNaN(Number(this.list[val2.$index].ADD_ITEM_AMOUNT))) {
        this.list[val2.$index].RECEIVE_AMOUNT = 0;
      } else {
        this.list[val2.$index].RECEIVE_AMOUNT = Number(this.list[val2.$index].DISCOUNT) * Number(this.list[val2.$index].ADD_ITEM_AMOUNT);
      }
      const targetKey = Object.keys(val)[0].split('|')[1];
      if(val2.CHARGE_PARTITION_CODE == 'S' || val2.CHARGE_PARTITION_CODE == 'N'){
        val2.RECEIVE_AMOUNT = 0.00;
      }
      //  这里就开始计算total   用来储存
      this.total = 0;
      this.list.forEach(x => {
        this.total = this.total + Number(x.RECEIVE_AMOUNT);
      });
      this.$emit('handlefjxmTotal', this.total);
    },
    // 获取附加项目方法
    async detailsFjxm() {
      let res = await detailsFjxm(_this.detaildata);
      // ADD_ITEM_AMOUNT附加项目费
      // DISCOUNT折扣率
      // RECEIVE_AMOUNT折扣后附加项目费
      res.data.rows.forEach(x => {
        if (isNaN(Number(x.DISCOUNT)) || isNaN(Number(x.ADD_ITEM_AMOUNT))) {
          x.RECEIVE_AMOUNT = 0;
        } else {
          x.RECEIVE_AMOUNT = Number(x.DISCOUNT) * Number(x.ADD_ITEM_AMOUNT);
        }
        if(x.CHARGE_PARTITION_CODE == 'S'||x.CHARGE_PARTITION_CODE == 'N'){
          x.RECEIVE_AMOUNT = 0;
        }
      });
      _this.$refs.table.$refs.pageTable.EXECUTE_INSERT(res.data.rows);
    },
    closeAddfjxm(val) {
      this.visibleFjxm = false;
      if (val != false) {
        val.DISCOUNT = 1;
        if (isNaN(Number(val.DISCOUNT)) || isNaN(Number(val.ADD_ITEM_AMOUNT))) {
          val.RECEIVE_AMOUNT = 0;
        } else {
          val.RECEIVE_AMOUNT = Number(val.DISCOUNT) * Number(val.ADD_ITEM_AMOUNT);
        }
        if(val.CHARGE_PARTITION_CODE == 'S' || val.CHARGE_PARTITION_CODE == 'N'){
          val.RECEIVE_AMOUNT = 0.00;
        }
        this.$refs.table.$refs.pageTable.EXECUTE_INSERT(val);
        //  这里就开始计算total   用来储存
        this.total = 0;
        this.list.forEach(x => {
          this.total = this.total + Number(x.RECEIVE_AMOUNT);
        });
        this.$emit('handlefjxmTotal', this.total);
      }
    },
    ceshi(val) {
      _this.$emit('changethree', _this.list);
    },
    addFjxmHandle() {
      this.visibleFjxm = true;
    },
    // 传给父组件的数据
    syncTableFun(val) {
      this.tableData = val;
    },
    // 维修材料表单数据删除
    delete(val) {
      this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$refs.table.$refs.pageTable.EXECUTE_DELETE([val]);
          this.fetchParams = { ...this.fetchParams };
          //  这里就开始计算total   用来储存
          this.total = 0;
          this.list.forEach(x => {
            this.total = this.total + Number(x.RECEIVE_AMOUNT);
          });
          // 再写个方法传删除数据
          if (val.ITEM_ID != null && val.ITEM_ID != '' && val.ITEM_ID != undefined) {
            this.$emit('handleDeleted', val);
          }
          this.$emit('handlefjxmTotal', this.total);
        })
        .catch(() => {});
    },
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          render: props => {
            // return <span>{props.row.index + 1}</span>;
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '操作',
          fixed: 'left',
          dataIndex: 'column-action',
          render: props => {
            return (
              <el-button size="mini" type="text" onClick={() => this.delete(props.row)}>
                删除
              </el-button>
            );
          }
        },
        {
          title: '附加项目代码',
          dataIndex: 'ADD_ITEM_CODE',
          sorter: true
        },
        {
          title: '附加项目名称',
          dataIndex: 'ADD_ITEM_NAME',
          sorter: true
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          sorter: true,
          // filterItems:[],
          editable: true,
          editType: 'select',
          editItems: [],
          editRequired: true
          // render: props => {
          //   return <span>{this.func(props.row.CHARGE_PARTITION_CODE,4)}</span>;
          //   }
        },
        {
          title: '附加项目费',
          dataIndex: 'ADD_ITEM_AMOUNT',
          sorter: true,
          editable: true,
          editType: 'number',
          min: 0,
          precision: 2
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          sorter: true,
          editable: true,
          editType: 'number',
          max: 1,
          min: 0,
          precision: 2
        },
        {
          title: '折扣后附加项目费',
          dataIndex: 'RECEIVE_AMOUNT',
          precision: 2,
          sorter: true
        }
        // {
        //   title: '附加项目ID',
        //   dataIndex: 'ITEM_ID',
        //   sorter: true
        // }
      ];
    },
    // 收费区分下拉框
    async getPartitionList() {
      const res = await getPartitionList();
      this.columns.find(x => x.dataIndex === 'CHARGE_PARTITION_CODE').editItems = res.data.map(x => ({ value: x.CHARGE_PARTITION_CODE, text: x.CHARGE_PARTITION_NAME }));
    },
    async getFwhdFjxmList(row) {
      const res = await getFwhdFjxmList(row, {
        creditTimes: 1.0,
        afterlabour: 0,
        zkAmountLabour: 0,
        afterDiscountAmountPart: 0,
        zkAmountPart: 0,
        fag: 'U',
        labourList: '',
        partList: '',
        subjoinList: '',
        modelList: '',
        vehicleList: '',
        order: 'asc',
        limit: 10,
        offset: 0,
        dmsFuncId: 35100010,
        _: 1565681747515
      });
      this.list = res.rows;
    },
    async getFwhdSfList() {
      const res = await getFwhdSfList();
      this.columns[4].filterItems = res.map(x => ({ value: x.CHARGE_PARTITION_CODE, text: x.CHARGE_PARTITION_NAME }));
    },
    func(val, num) {
      if (val) {
        const res = this.columns[num].filterItems.find(x => x.value == val);
        return res.text;
      }
    }
  }
};
</script>
